//
//  JSSRetweetStatusView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/21.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class JSSRetweetStatusView: UIView {
    
    //转发微博的底部约束(根据是否有配图更改约束)
    var bottomContrait:Constraint?
    
    var statusViewModel:JSSStatusViewModel?{
        
        didSet{
            
            //设置转发微博的内容
           // contentLable.text = statusViewModel?.statusModel?.retweeted_status?.text
            
          // contentLable.text = statusViewModel?.retweetText
            contentLable.attributedText = statusViewModel?.retweetAttrString
            
            //涉及到三方约束关系 要更新约束 必须先要让先前的约束失效
            bottomContrait?.uninstall()
            
            //判断转发微博是否有配图
            //如果有配图
            if let pic_urls = statusViewModel?.statusModel?.retweeted_status?.pic_urls where pic_urls.count > 0{
                
                
                //判如果图片是一张图片 设置itemSize为配图的大小
                if pic_urls.count == 1 {
                    //获取flowLayout
                    let flowLayout = picImageView.collectionViewLayout as? UICollectionViewFlowLayout
                    //更换collectionView的itemsize
                    flowLayout?.itemSize = statusViewModel!.retweetStatusViewPictureSize
                    //print("一张图\(flowLayout?.itemSize)")
                    
                }else {
                    
                    //否则设置成为正方形
                    let flowLayout = picImageView.collectionViewLayout as? UICollectionViewFlowLayout
                    flowLayout?.itemSize = CGSize(width: JSSPictureInfoViewItemWH, height: JSSPictureInfoViewItemWH)
                    //print("多张图\(flowLayout?.itemSize)")
                }

 
                // 1.设置数据(更新先前配图的(size)大小宽度)
                picImageView.snp_updateConstraints(closure: { (make) -> Void in
                    make.size.equalTo(statusViewModel!.retweetStatusViewPictureSize)
                })
                
                //给配图视图赋值
                picImageView.pic_urls = pic_urls
                // 2.显示视图
                picImageView.hidden = false
                
                // 3.更新约束(让转发微博的底部约束等于配图的底部约束加一个间距)
                self.snp_makeConstraints(closure: { (make) -> Void in
                   bottomContrait =  make.bottom.equalTo(picImageView.snp_bottom).offset(JSSStatusMargin).constraint
                })
                

            }else{
            
                //如果没有配图
                // 1.隐藏视图
                picImageView.hidden = true
                // 2.更新约束(让转发微博的鼻部约束等于内容底部的约束加一个间距)
                self.snp_makeConstraints(closure: { (make) -> Void in
                   bottomContrait = make.bottom.equalTo(contentLable.snp_bottom).offset(JSSStatusMargin).constraint
                })
            }
            
            
            
        }
    }

   override init(frame: CGRect) {
        super.init(frame: frame)
    
     setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
       
       backgroundColor =  UIColor(white: 237/255.0, alpha: 1)
//         backgroundColor =  UIColor.whiteColor()
        //把微博内容添加到转发微博的VIew上
        addSubview(contentLable)
         //把微博配图添加到转发微博的VIew上
        addSubview(picImageView)
        
        contentLable.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(JSSStatusMargin)
        }
        
        picImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLable.snp_bottom).offset(JSSStatusMargin)
            make.leading.equalTo(contentLable)
            //暂时写死 看效果
            make.size.equalTo(CGSizeMake(80, 80))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            //记录转发微博(当前类)的底部约束
           bottomContrait = make.bottom.equalTo(contentLable.snp_bottom).offset(JSSStatusMargin).constraint
        }
    
    }
    
    //懒加载转发微博的内容
    private lazy var contentLable:YYLabel = {
       
//        let lable = UILabel(textColor: UIColor.darkGrayColor(), textFont: JSSStatusContentFont, preferredMaxLayoutWidth: SCREENW - 2*JSSStatusMargin)
        
        let lable = YYLabel()
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(JSSStatusContentFont)
        lable.preferredMaxLayoutWidth = SCREENW - 2*JSSStatusMargin
        lable.numberOfLines = 0
        
        lable.userInteractionEnabled = true
        
        return lable
        
    }()
    
    //懒加载转发微博的图片
    private lazy var picImageView:JSSPictureInfoView = JSSPictureInfoView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

}
